package cn.luckdou.system.service.impl.global;

import cn.luckdou.system.model.global.GlobalYo;
import cn.luckdou.system.service.global.IGlobalService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import cn.luckdou.system.model.global.Global;
import cn.luckdou.system.mapper.global.GlobalMapper;
import cn.luckdou.system.model.global.GlobalVo;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 全球地区库 服务实现类
 * </p>
 *
 * @author Dadou
 * @since 2017-10-09
 */
@Service
public class GlobalServiceImpl extends ServiceImpl<GlobalMapper, Global> implements IGlobalService {
    @Resource
    private GlobalMapper globalMapper;

    /**
     * 查询所有的数据（带子父级关系）
     * @param global
     * @return
     * @throws Exception
     */
    public List<GlobalVo> findAllGlobalsAndChildren(Global global) throws Exception{
        ArrayList<GlobalVo> globalVos = new ArrayList<>();
        EntityWrapper<Global> wrapper = new EntityWrapper<>();
        Map<Integer, List<GlobalVo>> threeListMap = selectAllThreeLevelChildren();
        wrapper.setEntity(global);
        //查询所有的数据  接下来就是封装
        wrapper.eq("level",1);
        List<Global> globals = globalMapper.selectList(wrapper);
        for (Global oneglobal : globals
                ) {
            GlobalVo oneglobalVo = new GlobalVo(oneglobal);
            //接下来就是设置children
            EntityWrapper<Global> twowrapper = new EntityWrapper<>();
            twowrapper.eq("pid",oneglobal.getId());
            List<Global> twochildren = globalMapper.selectList(twowrapper);
            //接下来就是设置这些为二级目录的children
            ArrayList<GlobalVo> twoglobals = new ArrayList<>();
            for (Global twoglobal:twochildren
                    ) {
                GlobalVo twoglobalVo = new GlobalVo(twoglobal);
                List<GlobalVo> globals1 = threeListMap.get(twoglobal.getId());
                twoglobalVo.setChildren(globals1);
                twoglobals.add(twoglobalVo);
            }
            oneglobalVo.setChildren(twoglobals);
            globalVos.add(oneglobalVo);
        }


        return  globalVos;
    }

    /**
     * 查询所有的三级数据 并按照PId 进行分组
     * @return
     * @throws Exception
     */
    public Map<Integer,List<GlobalVo>> selectAllThreeLevelChildren() throws Exception{
        EntityWrapper<Global> wrapper = new EntityWrapper<>();
        wrapper.eq("level",3);
        List<Global> globals = globalMapper.selectList(wrapper);
        Map<Integer, List<GlobalVo>> resultMap = new HashMap<Integer, List<GlobalVo>>();
        for(Global global : globals){
            if(resultMap.containsKey(global.getPid())){//map中异常批次已存在，将该数据存放到同一个key（key存放的是异常批次）的map中
                GlobalVo globalVo = new GlobalVo(global);
                resultMap.get(global.getPid()).add(globalVo);
            }else{//map中不存在，新建key，用来存放数据
                List<GlobalVo> tmpList = new ArrayList<GlobalVo>();
                GlobalVo globalVo = new GlobalVo(global);
                tmpList.add(globalVo);
                resultMap.put(global.getPid(), tmpList);

            }

        }
        return  resultMap;
    }
    //进行准换的方法
    public List<GlobalYo> caseToYo(List<GlobalVo> globalVos){
        ArrayList<GlobalYo> globalYos = new ArrayList<>();
        for (GlobalVo globalVo:globalVos
             ) {
            GlobalYo globalYo = new GlobalYo();
            globalYo.setId(Long.valueOf(globalVo.getId()));
            globalYo.setName(globalVo.getName());
            //接下来设置children
            List<GlobalVo> children = globalVo.getChildren();
            ArrayList<GlobalYo> globalYos1 = new ArrayList<>();
            for (GlobalVo globalVo1:children
                 ) {
                GlobalYo globalYo1 = new GlobalYo();
                globalYo1.setId(Long.valueOf(globalVo1.getId()));
                globalYo1.setName(globalVo1.getName());
                List<GlobalVo> children1 = globalVo1.getChildren();
                ArrayList<GlobalYo> globalYos2 = new ArrayList<>();
                if(null!=children1&&children1.size()>0) {
                    for (GlobalVo globalVo2 : children1
                            ) {
                        GlobalYo globalYo2 = new GlobalYo();
                        globalYo2.setId(Long.valueOf(globalVo2.getId()));
                        globalYo2.setName(globalVo2.getName());
                        globalYos2.add(globalYo2);
                    }
                }
                globalYo1.setChildren(globalYos2);
                globalYos1.add(globalYo1);
            }
            globalYo.setChildren(globalYos1);
            globalYos.add(globalYo);
        }
        return  globalYos;
    }

}
